var vehicleVO,lastID;
var veharray;
var thisObject;
var currentPage;
var currentSearchString = '';
var currentCriteria = '';
var currentSortField = 'update_date';
var currentSortType = 0;
var currentStatus = 'New Submission';
var currentVehicle = 0;
var infoWindow;
var myObject = '';
var vehsingle;
var currentVCount = 0;
var pagLimit = 10;
var booleansavedet;
var vehicleCounts;
var lockedData;
var mode = '';

var VehicleProxy = Objs("model.VehicleProxy",
	Proxy,
{
	initialize: function( name,lists)
	{ 
		VehicleProxy.$super.initialize.call( this, name,lists);
        thisObject=this;
		//this.getVehicleByPage(1);
	},
	setSortSettings: function(fieldName, sortTpe)
	{
		currentSortField = fieldName;
		currentSortType = sortTpe;
                thisObject.sendNotification( NotificationNames.CLEAR_LIST, 'clear' );
		this.common();
	  	this.getCount();
	},
	refreshVehicle: function()
	{
		currentSearchString = '';
		currentCriteria = '';
		this.getVehicleByPage(currentPage);
	},
	getVehicleByPage:function(page) //the parameter id(tow user id) is using to list tow vehicle added by mufeed 27/09/2012
    {
          
		thisObject.sendNotification( NotificationNames.CLEAR_LIST, 'clear' );
		currentPage=page;
        this.common();
        if(global_user.admin_type!="Tower Administrator")
            this.getCount();	       
    },
	getDefaultStage: function()
	{
		currentSearchString = '';
		currentCriteria = '';
		currentSortField = 'update_date';
		currentSortType = 0;
	},
    getVehicleTable:function()
    {
       return veharray;
    },
	getLockedTable:function()
    {
       return lockedData;
    },
	getLockedTable:function()
    {
       return lockedData;
    },
	getVehicleCount:function()
    {
       return currentVCount;
    },
	getMode:function()
    {
       return mode;
    },
	getLimit:function()
    {
       return pagLimit;
    },
	getPageStatus:function()
    {
       var pStatus = '';
	   var startIndex = 0;
	   var endIndex = 0;
	   if(currentPage*pagLimit > currentVCount)
	   {
		   endIndex = (currentVCount%pagLimit) + ((currentPage - 1)*pagLimit);
	   }
	   else
	   {
		   endIndex = pagLimit + ((currentPage-1)*pagLimit);
	   }
	   
	   if( (currentPage-1) > 0)
	   {
		   startIndex = ((currentPage-1)*pagLimit);
	   }
	   else
	   {
		   startIndex = 1;
	   }
           var status;
           if(currentStatus==1)
               status="New";
           else if(currentStatus==2)
                status="In Progress";
           else if(currentStatus==3)
               status="Delivered";
           else
               status=currentStatus;
           
	   pStatus = status + " Vehicles " + startIndex + " - " + endIndex + " / " + currentVCount;
	   return pStatus;
    },
	setMode:function(changeMode)
    {
	
       mode = changeMode;
    },
	getVehicleSingle:function()
    {
       	return vehsingle;
    },
     setStatus_list:function(veh_id,statusname)
	 {
	  var statobj=new Object();
	  statobj.vehicle_id=veh_id;
	  statobj.status=statusname;
	  statobj.adminid=global_user.admin_id;
	   $.ajax({
		type: "POST",
		data:statobj,
		url:"../services/JqueryVehicleservice.php?action=changestatus",
		success: function(data,status) {
		if(statusname=="Deleted")
			thisObject.sendNotification( NotificationNames.DELETE, statusname);
		else
			thisObject.sendNotification( NotificationNames.CHANGE_STATUS, statusname);			
		}
		});
	 },
     
    
	getCount:function()
	{

      $.ajax({
		type: "POST",
		url:"../services/JqueryVehicleservice.php?action=getcount",
		success: function(data,status) {
			var data= eval('(' + data + ')');
			vehicleCounts = new Object();
			vehicleCounts.nscount = data.countnum[0];
			vehicleCounts.bacount = data.countnum[1];
			vehicleCounts.nipcount = data.countnum[4];
			vehicleCounts.nsMsgcount = data.countnum[2];
			vehicleCounts.baMsgcount = data.countnum[3];
			vehicleCounts.nipMsgcount = data.countnum[5];
			thisObject.sendNotification( NotificationNames.GETCOUNT_READY, 'count' );
			}
		});
	},
	addInfoComment:function(currespondence)
	{	
		currespondence.messageOwener = global_user.admin_name;
		currespondence.messageRecipient = currentemail;
		currespondence.vehicleId = currentid;
		
		currespondence.userType = global_user.admin_type;
		currespondence.messageTo = currentname;
		currespondence.isRead = 'true';
	
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=addMessages",
			data:currespondence,
			success: function(data,status) {
				var mData= eval('(' + data + ')');
						
				thisObject.sendNotification( NotificationNames.ADD_MESSAGE_SUCCESS, mData );	
				//thisObject.sendNotification(NotificationNames.ADD_MESSAGE_SUCCESS_COMMENT, mData );	
			}
		});
	},
	getMessages:function()
	{
	 var object=new Object();
	 object.vid=currentid;
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=getMessages",
			data:object,
			success: function(data,status) {
				var mData= eval('(' + data + ')');
						
				thisObject.sendNotification( NotificationNames.LIST_MESSAGE, mData );	
				//thisObject.sendNotification(NotificationNames.ADD_MESSAGE_SUCCESS_COMMENT, mData );	
			}
		});
	},
	saveVehicleDetails:function(nVehicle)
	{  
	
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=update",
			data:nVehicle,
			success: function(data,status) {
				var data= eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.SAVE_SUCCESS, data );
			}
		});
	},
	getprivatenotes:function()
	{
	var object=new Object();
	 object.vid=currentid;
			$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=getPnotes",
			data:object,
			success: function(data,status) {
			var data= eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.LIST_PRIVATE, data );
			}
			});
	},
	addPrivateNote:function(pnote)
	{ 
		var now=new Date();
		var curr_date = now.getDate();
		var curr_month = now.getMonth() + 1; //Months are zero based
		var curr_year = now.getFullYear();
		var curr_hours = now.getHours()
		var curr_minutes = now.getMinutes()
		var curr_seconds = now.getSeconds();
		if(curr_month<10)
			curr_month="0"+curr_month;
		if(curr_date<10)
			curr_date="0"+curr_date;
		var cu_date=curr_year+"-"+curr_month+"-"+curr_date+" "+curr_hours+":"+curr_minutes+":"+curr_seconds;
		pnote.dateTime=cu_date;
		pnote.messageOwener = global_user.admin_name;
	
		pnote.userType = global_user.admin_type;
		pnote.messageTo = '';
		pnote.isRead = 'true';
		if(mode==AppConstants.APP_ADD)
		{
			pnote.vehicleId = 0;
			pnote.messageRecipient = '';
		}
		else
		{
			pnote.messageRecipient =currentemail;
			pnote.vehicleId =currentid;
		}
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=addPnotes",
			data:pnote,
			success: function(data,status) {
				if(mode==AppConstants.APP_ADD)
				{
					pnote.data=data;
					
					thisObject.sendNotification( NotificationNames.ADDNEW_PNOTE_SUCCESS, pnote );
				}
				else
				{
					var array=new Array()
					array.push(pnote);
					for(i=0;i<currentnotes.length;i++)
						array.push(currentnotes[i]);
					if(vehicleSingle)
					{
					vehicleSingle.private_notes=array;
					currentnotes=array;
					}
					else
					currentnotes=array;
					thisObject.sendNotification( NotificationNames.ADD_PNOTE_SUCCESS, pnote );
				}
			}
		});
	},
	updateVehicle: function(vehicleData)
	{
		console.log(vehicleData);
		var vehicle = new Object();			
		vehicle = this.changeToObject(vehicleData);
		
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=update",
			data:vehicle,
		 	success: function(data,status) {
		 
				var data= eval('(' + data + ')');
				//window.location="vehicle_list.php";
			 	thisObject.sendNotification( NotificationNames.SAVE_SUCCESS, data );
				//alert("S");
			}

		});
	},
	getreminderbyid:function()
	{
		var remidobj=new Object();
		remidobj.vehicle_id=currentid;
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=getrembyid",
			data:remidobj,
			success: function(data,status) {
				var	Reminder= eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.REMDET, Reminder );	
			}
		});
	},
	updateWatchlist:function()
	{ 
		var remobject=new Object();
		remobject.vehicle_id=currentid;
		remobject.to_date=document.getElementById("datereminder").value;
		remobject.remind_note=document.getElementById("remnote").value;
		remobject.isWatch=1;
		remobject.remind_by=global_user.admin_name;
		remobject.set_by=global_user.admin_id;
		remobject.isReminder=1;
		
		if(thisreminder.vresult.watch_id)
		{
			this.updateWatch(remobject);
		}
		else
		{
			this.setWatch(remobject);
		}
	},
    setTowStatus:function(vehicle_id)
    {
        var objtow=new Object();
        objtow.vehicle_id=vehicle_id;
        objtow.tow_status = btn_tow_status;
            
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=setTowStatus",
			data:objtow,
			success: function(data,status) {
				switch(btn_tow_status)
				{
					case 1:
						$("#btn_towstatus").html("New Vehicle");
						
						$("#statuschange").show();
						$("#statuschange").html("Status changes to New Vehicle")				 
						setTimeout(function() {
								  $("#statuschange").hide()
							}, 10000);
						break;
					case 2: 
						$("#btn_towstatus").html("Scheduled");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Scheduled");
						setTimeout(function() {
						  $("#statuschange").hide()
						}, 10000);
						 break;
						
					case 3: 
						$("#btn_towstatus").html("Delivered");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Delivered")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break;
                                      case 5: 
						$("#btn_towstatus").html("Picked Up");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Picked Up")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break;
                                                        
                                    case 6: 
						$("#btn_towstatus").html("Unable to Contact");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Unable to Contact")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break;   
                                                         
                                    case 7: 
						$("#btn_towstatus").html("Cancelled");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Cancelled")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break;                        
				}   
			  
		 
			}
		});
    },
    
    setTowStatus_list:function(vehicle_id)
    {
        var objtow=new Object();
        objtow.vehicle_id=vehicle_id;
        objtow.tow_status = btn_tow_status;
            
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=setTowStatus",
			data:objtow,
			success: function(data,status) {
				switch(btn_tow_status)
				{
					case 1:
						//$("#tow_status_list_"+vehicle_id).html("New Vehicle");
						   $("#tow_status_list_"+vehicle_id).html("<span class='label label-warning'>New Vehicle <b class='caret'></b></span>");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to New Vehicle")				 
						setTimeout(function() {
								  $("#statuschange").hide()
							}, 10000);
						break;
					case 2: 
						//$("#tow_status_list_"+vehicle_id).html("Scheduled");
                                               
                                                 $("#tow_status_list_"+vehicle_id).html("<span class='label label-info'>Scheduled<b class='caret'></b></span>");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Scheduled");
						setTimeout(function() {
						  $("#statuschange").hide()
						}, 10000);
						 break;
						
					case 3: 
						
                                                $("#tow_status_list_"+vehicle_id).html("<span class='label label-success'>Delivered<b class='caret'></b></span>");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Delivered")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break;
                                        case 5: 
						$("#tow_status_list_"+vehicle_id).html("<span class='label'>Picked Up<b class='caret'></b></span>");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Picked Up")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break; 
                                        case 6: 
						$("#tow_status_list_"+vehicle_id).html("<span class='label'>Unable to Contact<b class='caret'></b></span>");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Unable to Contact")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break;   
                                        
                                        case 7: 
						$("#tow_status_list_"+vehicle_id).html("<span class='label'>Cancelled<b class='caret'></b></span>");
						$("#statuschange").show();
						$("#statuschange").html("Status changes to Cancelled")
						setTimeout(function() {
										  $("#statuschange").hide()
									}, 10000);
							 break; 
                                                         
                                                    
                                                         
                                                         
				}   
			  
		 
			}
		});
    },
        
	setWatch:function(remobject)
	{ 
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=setWatchList",
			data:remobject,
			success: function(data,status) {
				todate=document.getElementById("datereminder").value;
				remnote=document.getElementById("remnote").value;
				alert("Sucess");
				thisObject.sendNotification( NotificationNames.SAVEREM, 'li' );	
				}
		});
	},
    updateWatch:function(remobject)
	{ 
		
		remobject.watchid=thisreminder.vresult.watch_id;
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=updateWatchList",
			data:remobject,
			success: function(data,status) {
				todate=document.getElementById("datereminder").value;
				remnote=document.getElementById("remnote").value;
				alert("Sucess");
				thisObject.sendNotification( NotificationNames.SAVEREM, 'li' );	
			}
		});
	},
	saveWatchlist:function()
	{ 
	
	if(document.getElementById("datereminder").value && document.getElementById("remnote").value || (!$(jQuery("#checkbox-1")).attr('checked')))
	{
		var remobject=new Object();
		remobject.vehicle_id=currentid;
		if ($(jQuery("#checkbox-1")).attr('checked')) 
		{
			remobject.to_date=document.getElementById("datereminder").value;
			remobject.remind_note=document.getElementById("remnote").value;
			remobject.isReminder=1;
		}
		else
		{
			remobject.to_date='';
			remobject.remind_note='';
			remobject.isReminder=0;
		}
		remobject.isWatch=1;
		remobject.remind_by=global_user.admin_name;
		remobject.set_by=global_user.admin_id;
		this.setWatch(remobject);
	}
	else
	alert("Missing fields");
	},
	removeWatchlist:function()
	{
	
		var remobject=new Object();
		remobject.vehicle_id=currentid;
		remobject.remind_by= global_user.admin_name;
		remobject.set_by= global_user.admin_id;
		remobject.isWatch=0;	
		if(thisreminder.vresult.watch_id)
		{
			remobject.isReminder=1;
			remobject.watch_id=thisreminder.vresult.watch_id;
		}
		else
		{
			remobject.isReminder=0;
			remobject.watch_id="";
		}
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=removeWatchList",
			data:remobject,
			success: function(data,status) {
			alert("Watchlist Removed");
			thisObject.sendNotification( NotificationNames.SAVEREM, 'li' );	
		
			}
		});
	},
	changeToObject:function(vehicleVO)
	{   
		var vehicle = new Object();	
		vehicle.isNormal = vehicleVO.isNormal;
		vehicle.fadminname = global_user.admin_name;
		vehicle.admin_id = global_user.admin_id;
		vehicle.is_read= 1;
		vehicle.is_watch= vehicleVO.is_watch;
		vehicle.vehicle_id=vehicleVO.vehicle_id;
		vehicle.year= vehicleVO.year;
		vehicle.make=vehicleVO.make;
		vehicle.model=vehicleVO.model;
		vehicle.mileage= vehicleVO.mileage;
		vehicle.wrong=vehicleVO.wrong;
		vehicle.great=vehicleVO.great;
		vehicle.paper_work=vehicleVO.paper_work;
		vehicle.title_name=vehicleVO.title_name;
		vehicle.title_note=vehicleVO.title_note;
		vehicle.ad_source=vehicleVO.ad_source;
		vehicle.vin=vehicleVO.vin;
		
		vehicle.drivetrain_type=vehicleVO.drivetrain_type;
		vehicle.engine= vehicleVO.engine;
		vehicle.transmission= vehicleVO.transmission;
		/*contactinfo*/
		vehicle.first_name=vehicleVO.first_name;
		vehicle.bussiness_name=vehicleVO.business_name;
		vehicle.contact_name=vehicleVO.contact_name;
		vehicle.street_address= vehicleVO.street_address;
		vehicle.city= vehicleVO.city;
		vehicle.state= vehicleVO.state;
		vehicle.zip= vehicleVO.zip;
		vehicle.phone= vehicleVO.phone;
		vehicle.cell_phone= vehicleVO.cell_phone;
		vehicle.email=vehicleVO.email;
		vehicle.contact_me=vehicleVO.contact_me;
		vehicle.contact_note=vehicleVO.contact_note;
		vehicle.delivery=vehicleVO.delivery;
		vehicle.date= vehicleVO.date;
		vehicle.color= vehicleVO.color;
		vehicle.delivery_note= vehicleVO.delivery_note;
		vehicle.delivery_option= vehicleVO.delivery_option;
		vehicle.asking_price=  Number(vehicleVO.asking_price);
		vehicle.bid_price=  Number(vehicleVO.bid_price);
		vehicle.accepted_price=  Number(vehicleVO.accepted_price);
		/*Vlocation*/
		vehicle.v_street_address= vehicleVO.v_street_address;
		vehicle.v_city= vehicleVO.v_city;
		vehicle.v_state= vehicleVO.v_state;
		vehicle.v_zip= vehicleVO.v_zip;
		vehicle.located_at= vehicleVO.located_at;
		vehicle.i_want_to= vehicleVO.i_want_to;
		vehicle.status= document.getElementById('btn-status').innerHTML;
		vehicle.date=document.getElementById('remove_by').value;
		return vehicle;
	},
   common:function()
   { 
   
		var vehiclerequest=new Object();
		vehiclerequest.criteria = currentCriteria;
		vehiclerequest.search_key = currentSearchString;
		vehiclerequest.vehicle_count = 0;
		if(global_user.admin_type=="Tower Administrator")
		{  
			vehiclerequest.type = "tower";
			vehiclerequest.status = currentStatus;
			vehiclerequest.tower_company = global_user.admin_company;
          
        }  
		else
		{
          vehiclerequest.status = currentStatus;
		}
      
		vehiclerequest.num_record = pagLimit;
		vehiclerequest.index=(currentPage-1)*pagLimit;
		vehiclerequest.sort_type = currentSortType;
		vehiclerequest.sort_string = currentSortField;
      $.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=getvehicle",
			data:vehiclerequest,
			success: function(data,status) {
				myObject = eval('(' + data + ')');
				currentVCount = myObject.count;
				veharray=new Array();
				for(i=0;i<myObject.vehicleslist.length;i++)
				{ 
					vehicleVO=new VehicleVO();
					vehicleVO.is_read=myObject.vehicleslist[i].vehicle[0].is_read;
                     vehicleVO.tow_is_read=myObject.vehicleslist[i].vehicle[0].tow_is_read;
                                        
					vehicleVO.is_watch=myObject.vehicleslist[i].vehicle[0].is_watch;
					vehicleVO.vehicle_id=myObject.vehicleslist[i].vehicle[0].vehicle_id;
					vehicleVO.year=myObject.vehicleslist[i].vehicle[0].year;
					vehicleVO.make=myObject.vehicleslist[i].vehicle[0].make;
					vehicleVO.make_name=myObject.vehicleslist[i].vehicle[0].make_name;
					if(myObject.vehicleslist[i].vehicle[0].state_name==null)
						vehicleVO.state_name='';
					else
						vehicleVO.state_name=myObject.vehicleslist[i].vehicle[0].state_name;
					if(myObject.vehicleslist[i].vehicle[0].v_state_name==null)
						vehicleVO.v_state_name='';
					else	
						vehicleVO.v_state_name=myObject.vehicleslist[i].vehicle[0].v_state_name;
					vehicleVO.asking_price=myObject.vehicleslist[i].vehicle[0].asking_price;
					vehicleVO.model=myObject.vehicleslist[i].vehicle[0].model;
					
					vehicleVO.model_name=myObject.vehicleslist[i].vehicle[0].model_name;
					vehicleVO.mileage=myObject.vehicleslist[i].vehicle[0].mileage;
					vehicleVO.great=myObject.vehicleslist[i].vehicle[0].great;
					vehicleVO.wrong=myObject.vehicleslist[i].vehicle[0].wrong;
					vehicleVO.paper_work=myObject.vehicleslist[i].vehicle[0].paper_work;
					
					vehicleVO.title_name=myObject.vehicleslist[i].vehicle[0].title_name;
					vehicleVO.title_note=myObject.vehicleslist[i].vehicle[0].title_note;
					vehicleVO.ad_source=myObject.vehicleslist[i].vehicle[0].ad_source;
					vehicleVO.vin=myObject.vehicleslist[i].vehicle[0].vin;
					vehicleVO.drivetrain_type=myObject.vehicleslist[i].vehicle[0].drivetrain_type;
					vehicleVO.engine=myObject.vehicleslist[i].vehicle[0].engine;
					vehicleVO.transmission=myObject.vehicleslist[i].vehicle[0].transmission;
					
					vehicleVO.first_name=myObject.vehicleslist[i].vehicle[0].first_name;
					vehicleVO.business_name=myObject.vehicleslist[i].vehicle[0].business_name;
					vehicleVO.contact_name=myObject.vehicleslist[i].vehicle[0].contact_name;
					vehicleVO.street_address=myObject.vehicleslist[i].vehicle[0].street_address;
					vehicleVO.city=myObject.vehicleslist[i].vehicle[0].city;
					vehicleVO.state=myObject.vehicleslist[i].vehicle[0].state;
					vehicleVO.zip=myObject.vehicleslist[i].vehicle[0].zip;
					vehicleVO.phone=myObject.vehicleslist[i].vehicle[0].phone;
					vehicleVO.cell_phone=myObject.vehicleslist[i].vehicle[0].cell_phone;
					vehicleVO.e_mail=myObject.vehicleslist[i].vehicle[0].email;
                                     
					vehicleVO.contact_method=myObject.vehicleslist[i].vehicle[0].contact_me;
					vehicleVO.seller_note=myObject.vehicleslist[i].vehicle[0].contact_note;
					
					vehicleVO.v_street_address=myObject.vehicleslist[i].vehicle[0].v_street_address;
					vehicleVO.v_city=myObject.vehicleslist[i].vehicle[0].v_city;
					vehicleVO.v_state=myObject.vehicleslist[i].vehicle[0].v_state;
					vehicleVO.v_zip=myObject.vehicleslist[i].vehicle[0].v_zip;
					
					vehicleVO.delivery=myObject.vehicleslist[i].vehicle[0].delivery;
					//vehicleVO.remove_by=myObject.vehicleslist[i].vehicle[0].remove_by;
					vehicleVO.transport_note=myObject.vehicleslist[i].vehicle[0].transport_note;
					vehicleVO.i_want_to=myObject.vehicleslist[i].vehicle[0].i_want_to;
					vehicleVO.delivery_option=myObject.vehicleslist[i].vehicle[0].delivery_option;
					
					vehicleVO.status=myObject.vehicleslist[i].vehicle[0].status;
					
					vehicleVO.asking_price=myObject.vehicleslist[i].vehicle[0].asking_price;
					vehicleVO.bid_price=myObject.vehicleslist[i].vehicle[0].bid_price;
					vehicleVO.accepted_price=myObject.vehicleslist[i].vehicle[0].accepted_price;
					
                                     
					vehicleVO.vehicle_role = myObject.vehicleslist[i].vehicle[0].vehicle_role;
					
					vehicleVO.current_action=myObject.vehicleslist[i].vehicle[0].current_action;
					vehicleVO.update_date=myObject.vehicleslist[i].vehicle[0].update_date;
					vehicleVO.date=myObject.vehicleslist[i].vehicle[0].date;
					vehicleVO.assist_by = myObject.vehicleslist[i].vehicle[0].assist_by;
					vehicleVO.tow_status_name =myObject.vehicleslist[i].vehicle[0].tow_status_name;
					vehicleVO.messages=[];
                      for(j=0;j<myObject.vehicleslist[i].vehicle[0].messages.length;j++)
							{  
									vehicleVO.messages.push(myObject.vehicleslist[i].vehicle[0].messages[j]);
							}
                  
                                        if(myObject.vehicleslist[i].vehicle[0].removal_date=="null")
                                            vehicleVO.remove_by='';
					else	
					    vehicleVO.remove_by=myObject.vehicleslist[i].vehicle[0].removal_date;

					if(myObject.vehicleslist[i].vehicle[0].reminder_1[0]){
						vehicleVO.isReminder = 1;
					}else{
						vehicleVO.isReminder = 0;
					}
					if(myObject.vehicleslist[i].vehicle[0].images[0])
					{
						var url="../"+myObject.vehicleslist[i].vehicle[0].images[0].thumpUrl;
					}
					else
					{
						url="../assets/images/noImage.png"
					}
					vehicleVO.imageurl=url;
					veharray.push(vehicleVO);
				}
				thisObject.sendNotification( NotificationNames.INTIAL_VLIST, 'get' );	
			}
		});

    },
    
       /* getAmPm: function(removetime)
        {
            if(hours>11)
            {format="PM";
            }
            if (hours   > 12) { hours = hours - 12; }
            if (hours   == 0) { hours = 12; }  
            if (minutes < 10){
                minutes = "0" + minutes;
             }
            document.write("Time : " + hours + " : " + minutes + " : " + seconds + " " +format);
        },*/
        
	searchWiseVehicleList: function(searchString, criteria)
	{
		currentSearchString = searchString;
		currentCriteria = criteria;
		this.getVehicleByPage(1);
	},
	addNewVehicle:function(newVehicle)
	{ 	
		var vehicle = new Object();			
		vehicle = this.changeToObject(newVehicle);
		vehicle.privatenotesid=privatenotesid;
		
		$.ajax({
			type: "POST",
			url:"../services/JqueryAddNewVehicle.php?action=createbyadmin",
			data:vehicle,
			success: function(data,status) {
				thisObject.sendNotification( NotificationNames.ADD_NEW_SUCCESS, data );
			}
		});
	},
	unlockVehicle:function(id)
	{ 
		var idlast=new Object();
		idlast.vehicle_id=id;
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=unlock",
			data:idlast,
			success:function(result)
			{ 
				var resultJSONObj = eval('(' + result + ')');
				if(resultJSONObj.status == true)
				{
					alert("Unlock success");
					thisObject.getLockedVehicles(); 
				}
				else
					alert("Failed to unlock vehicle");
				thisObject.sendNotification( NotificationNames.UNLOCK_SUCCESS, 'locked' );	
			}
		});
	},
	unlockOpenVehicle:function()
	{ 
		var idlast=new Object();
		idlast.vehicle_id=currentVehicle;
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=unlock",
			data:idlast,
			success:function()
			{ 
					
			}
		});
		this.getVehicleByPage(1);
	},
	getLockedVehicles:function()
    {   
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=getlocked",
			success: function(data,status) {
				lockedData = eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.GET_LOCKED, lockedData );	
			}
		});
	},
	/*shareVehicle:function()
	{
		var sharebyemailobject=new Object();
		sharebyemailobject.vehicle_id=currentid;
		sharebyemailobject.admin_id=global_user.admin_name;
		
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleService.php?action=SharebyUnknownEmail",
			data:sharebyemailobject,
			success: function(data,status) {
	
				if(data)
				{
					alert(data);
				}
				else
				{
					alert("Vehicle Shared Successfully");
				}
				
			}
		});
	
	},*/
	shareVehicle:function()
	{
		var sharebyemailobject=new Object();
		sharebyemailobject.vehicle_id=currentid;
	
		sharebyemailobject.partner_id=partnerid;
		
		sharebyemailobject.note=$("#anynote").val();
		
		sharebyemailobject.admin_id=global_user.admin_id;
		if ($(jQuery("#useanother")).attr('checked')) {
			sharebyemailobject.user_type='';
			sharebyemailobject.send_to=$("#anoemail").val();
		}
		else
		{
			
			sharebyemailobject.user_type=usertype;
			sharebyemailobject.send_to=$("#to").val();
		}
		if ($(jQuery("#cardetcheck")).attr('checked')) 
			sharebyemailobject.is_car_details=1;
		else	
			sharebyemailobject.is_car_details=0;
		if ($(jQuery("#sellerdetcheck")).attr('checked')) 
			sharebyemailobject.is_seller_details=1;
		else	
			sharebyemailobject.is_seller_details=0;
		if ($(jQuery("#privatnotcheck")).attr('checked')) 
			sharebyemailobject.is_tow_details=1;
		else	
			sharebyemailobject.is_tow_details=0;
		if ($(jQuery("#towdetcheck")).attr('checked')) 
			sharebyemailobject.is_map_details=1;
		else	
			sharebyemailobject.is_map_details=0;
		if ($(jQuery("#mapcheck")).attr('checked')) 
			sharebyemailobject.is_private_details=1;
		else	
			sharebyemailobject.is_private_details=0;
		if ($(jQuery("#useanother")).attr('checked')) {
			$.ajax({
				type: "POST",
				url:"../services/JqueryVehicleService.php?action=SharebyUnknownEmail",
				data:sharebyemailobject,
				success: function(data,status) {
				
					if(data)
					{
						alert(data);
					}
					else
					{
						alert("Vehicle Shared Successfully");
						
						$('#share-modal').modal('hide');
					}
					
				}
			});
		}
		else
		{
			$.ajax({
				type: "POST",
				url:"../services/JqueryVehicleService.php?action=SharebyEmail",
				data:sharebyemailobject,
				success: function(data,status) {
				
					if(data)
					{
						alert(data);
					}
					else
					{
						alert("Vehicle Shared Successfully");
						$('#share-modal').modal('hide');
					}
					
				}
			});
		}
	},
	getPartnerlist:function()
	{
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleService.php?action=getPartner",
			success: function(data,status) {
				var partner= eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.PARTNERLIST, partner );
				
			}
		});
	},
	getadminlist:function()
	{
		$.ajax({
			type: "POST",
			url:"../services/JqueryAdminservice.php?action=getadminlist",
			success:function(data,status)
			{ 
				var alistdata= eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.ADMINLIST, alistdata );
			}
		});
	},
	getreports:function(fromDate, toDate)
	{

		var fromtodate=new Object();
		fromtodate.from=fromDate;
		fromtodate.to=toDate;
		$.ajax({
			type: "POST",
			data:fromtodate,
			url:"../services/JqueryVehicleservice.php?action=getreport",
			success:function(data,status)
			{ 
				dataJSON= eval('(' + data + ')');
				thisObject.sendNotification( NotificationNames.REPORT, dataJSON );	
				
			}
		});
	},
	getForumComment:function()
	{
		fcurrentpage=1;
		this.commonForum();
	},
	commonForum:function()
	{  
		var Parentdet=new Object();
		Parentdet.pid='NULL';
		Parentdet.start_index=start_index;
		Parentdet.interval=interval;
		$.ajax({
			type: "POST",
			url:"../services/JqueryAdminservice.php?action=getforum",
			data:Parentdet,
			success:function(data,status)
			{ 
				fdata= eval('(' + data + ')');
				forumcount=fdata.totalcount;
				thisObject.sendNotification( NotificationNames.FORUM, fdata );	
			}
		});
	},
	getReminder:function()
	{
		//$.mobile.showPageLoadingMsg();
		$.ajax({
			type: "POST",
			url:"../services/JqueryVehicleservice.php?action=getreminder",
			success: function(dat,status) 
			{ 
				var jsonObject = eval('(' + dat + ')');
				var reminderbackarray=new Array();
				for(i=0;i<jsonObject.reminder.length;i++)
				{ 
					var remlogback = new ReminderVO();
					remlogback.watchid=jsonObject.reminder[i].watch_id;
					remlogback.vehicleid=jsonObject.reminder[i].vehicle_id;
                                        remlogback.year=jsonObject.reminder[i].year    ;
                                        remlogback.make=jsonObject.reminder[i].make;
                                        remlogback.model=jsonObject.reminder[i].model;
                                        
					remlogback.setby=jsonObject.reminder[i].set_by;
					remlogback.setdate=jsonObject.reminder[i].set_date;
					remlogback.todate=jsonObject.reminder[i].to_date;
					remlogback.remindnote=jsonObject.reminder[i].remind_note;
                                        remlogback.due=jsonObject.reminder[i].due;
                                        remlogback.duesoon=jsonObject.reminder[i].duesoon;
                                        
					//remlogback.remindby=jsonObject.reminder[i].remind_by;
                                        
					//remlogback.imageurl=jsonObject.reminder[i].image_path;
					//remlogback.count_isread=jsonObject.reminder[i].count_isread;
					reminderbackarray.push(remlogback);
				}
                              
				thisObject.sendNotification( NotificationNames.REMBACK, reminderbackarray );
			}
		});
	},
        checkLocked:function(index)
        {
            var reqData = new Object();
            reqData.veid =index;
          
            $.ajax({
                type:"POST",
                url:"../services/JqueryVehicleservice.php?action=checkVLocked",
                data:reqData,
                success: function(data,status){ 
                    var result = eval('(' + data + ')');
					if(result.status == "locked")
				         	  alert("The vehicle("+ index +") is locked by another user go back and try again ");
                                        else if(result.status == "open")
                                            window.location="info_page.php?id="+index; 
                                        else
                                            alert("Error can't check vehicle status");
					
                }
                
                
            });
        },
        
	getDetail:function(index)
	{ 
		mode =  AppConstants.APP_MODIFY;
		var reqData = new Object();
		vehsingle = new VehicleVO();
               
		//lastID = veharray[index].vehicle_id;
       //	reqData.veid = lastID;
         reqData.veid =index;
	   	reqData.adminname =global_user.admin_name;
	   	reqData.adminid = global_user.admin_id;
	   	reqData.companyid=global_user.admin_company
                
	   	if(global_user.admin_type=="Tower Administrator")
	   	{ 
	   		var url="../services/JqueryVehicleservice.php?action=gettowervbyid";
	   	}
	   	else
                {
                
                    
                        if(global_user.vehicle_role==8)
                              var url="../services/JqueryVehicleservice.php?action=gettowervbyid";
                         
                         else
                                 var url="../services/JqueryVehicleservice.php?action=getvbyid";
                     
	   	
	   	  }
        $.ajax({
			type: "POST",
			url:url,
			data:reqData,
	    	success: function(data,status) {
					vsingledata= eval('(' + data + ')');
						if(vsingledata.vsingle)
						{    
							vehsingle.is_read=vsingledata.vsingle.vehicle[0].is_read;
							vehsingle.tower_street=vsingledata.vsingle.vehicle[0].tower_street;
							vehsingle.tower_city=vsingledata.vsingle.vehicle[0].tower_city;
							vehsingle.tower_state=vsingledata.vsingle.vehicle[0].tower_state;
							if(vsingledata.vsingle.vehicle[0].state_name==null)
								vehsingle.state_name='';
							else
								vehsingle.state_name=vsingledata.vsingle.vehicle[0].state_name;
							if(vsingledata.vsingle.vehicle[0].v_state_name==null)
								vehsingle.v_state_name='';
							else	
								vehsingle.v_state_name=vsingledata.vsingle.vehicle[0].v_state_name;
							vehsingle.is_watch=vsingledata.vsingle.vehicle[0].is_watch;
							vehsingle.vehicle_id=vsingledata.vsingle.vehicle[0].vehicle_id;
							currentVehicle=vsingledata.vsingle.vehicle[0].vehicle_id;
							vehsingle.year=vsingledata.vsingle.vehicle[0].year;
							vehsingle.make=vsingledata.vsingle.vehicle[0].make;
							vehsingle.make_name=vsingledata.vsingle.vehicle[0].make_name;
							vehsingle.model_name=vsingledata.vsingle.vehicle[0].model_name;
							vehsingle.model=vsingledata.vsingle.vehicle[0].model;
							vehsingle.mileage=vsingledata.vsingle.vehicle[0].mileage;
							vehsingle.wrong=vsingledata.vsingle.vehicle[0].wrong;
							vehsingle.great=vsingledata.vsingle.vehicle[0].great;
							vehsingle.paper_work=vsingledata.vsingle.vehicle[0].paper_work;
							vehsingle.title_name=vsingledata.vsingle.vehicle[0].title_name;
							vehsingle.title_note=vsingledata.vsingle.vehicle[0].title_note;
							vehsingle.ad_source=vsingledata.vsingle.vehicle[0].ad_source;
							vehsingle.vin=vsingledata.vsingle.vehicle[0].vin;
							vehsingle.drivetrain_type=vsingledata.vsingle.vehicle[0].drivetrain_type;
							vehsingle.engine=vsingledata.vsingle.vehicle[0].engine;
							vehsingle.transmission=vsingledata.vsingle.vehicle[0].transmission;
							vehsingle.first_name=vsingledata.vsingle.vehicle[0].first_name;
							vehsingle.business_name=vsingledata.vsingle.vehicle[0].bussiness_name;
							vehsingle.created_date = vsingledata.vsingle.vehicle[0].created_date;

							vehsingle.created_by = vsingledata.vsingle.vehicle[0].created_by;

							vehsingle.contact_name=vsingledata.vsingle.vehicle[0].contact_name;
							vehsingle.contact_note=vsingledata.vsingle.vehicle[0].contact_note;
							vehsingle.street_address=vsingledata.vsingle.vehicle[0].street_address;
							vehsingle.city=vsingledata.vsingle.vehicle[0].city;
							vehsingle.state=vsingledata.vsingle.vehicle[0].state;
							vehsingle.zip=vsingledata.vsingle.vehicle[0].zip;
							vehsingle.phone=vsingledata.vsingle.vehicle[0].phone;
							vehsingle.cell_phone=vsingledata.vsingle.vehicle[0].cell_phone;
							vehsingle.email=vsingledata.vsingle.vehicle[0].email;
							vehsingle.contact_me=vsingledata.vsingle.vehicle[0].contact_me;
							vehsingle.contact_note=vsingledata.vsingle.vehicle[0].contact_note;
							vehsingle.tow_status_name = vsingledata.vsingle.vehicle[0].tow_status_name;


							vehsingle.delivery=vsingledata.vsingle.vehicle[0].delivery;
							vehsingle.delivery_note=vsingledata.vsingle.vehicle[0].delivery_note;
							vehsingle.delivery_option=vsingledata.vsingle.vehicle[0].delivery_option;

							if(vsingledata.vsingle.vehicle[0].removal_date=="null")
									vehsingle.remove_by='';
							else	
									vehsingle.remove_by=vsingledata.vsingle.vehicle[0].removal_date;

							vehsingle.date=vsingledata.vsingle.vehicle[0].date;
							vehsingle.color=vsingledata.vsingle.vehicle[0].color;


							/*Comments*/
							vehsingle.messages = [];
							vehsingle.private_notes = [];
							vehsingle.images = [];
							vehsingle.reminders = [];

							for(i=0;i<vsingledata.vsingle.vehicle[0].messages.length;i++)
							{  
									vehsingle.messages.push(vsingledata.vsingle.vehicle[0].messages[i]);
							}

							/*Pnotes*/
							for(i=0;i<vsingledata.vsingle.vehicle[0].pnotes.length;i++)
							{  
									vehsingle.private_notes.push(vsingledata.vsingle.vehicle[0].pnotes[i]);
							}
							/*Images*/
							for(i=0;i<vsingledata.vsingle.vehicle[0].images.length;i++)
							{  
									vehsingle.images.push(vsingledata.vsingle.vehicle[0].images[i]);
							}

							for(i=0;i<vsingledata.vsingle.vehicle[0].reminder_1.length;i++)
							{  
									vehsingle.reminders.push(vsingledata.vsingle.vehicle[0].reminder_1[i]);
							}
							/*Price*/
							vehsingle.asking_price=vsingledata.vsingle.vehicle[0].asking_price;
							vehsingle.bid_price=vsingledata.vsingle.vehicle[0].bid_price;
							vehsingle.accepted_price=vsingledata.vsingle.vehicle[0].accepted_price;
							/*Vlocation*/
							vehsingle.v_street_address=vsingledata.vsingle.vehicle[0].v_street_address;
							vehsingle.v_city=vsingledata.vsingle.vehicle[0].v_city;
							vehsingle.v_state=vsingledata.vsingle.vehicle[0].v_state;
							vehsingle.v_zip=vsingledata.vsingle.vehicle[0].v_zip;
							vehsingle.located_at=vsingledata.vsingle.vehicle[0].located_at;
							vehsingle.i_want_to=vsingledata.vsingle.vehicle[0].i_want_to;
							vehsingle.status=vsingledata.vsingle.vehicle[0].status;
							
							vehsingle.current_action=vsingledata.vsingle.vehicle[0].current_action;

							if(vsingledata.vsingle.vehicle[0].images[0])
							{
							var url="../"+vsingledata.vsingle.vehicle[0].images[0].thumpUrl;
							}

							vehsingle.imageurl=url;

							mode = AppConstants.APP_MODIFY;
							thisObject.sendNotification( NotificationNames.INFO_READY, 'info' );
						}  
						else
						 {
							$("#progressviewpage").hide();
							  $(".container").show();
							 $("#vehicle-content-info").hide();
							
							  $("#no-vehicle").show();
						 }   
		   			//}
				}
	  	});
	}
});
 